package com.ymttest.database.sqlwapper;

import com.ymt.core.tool.Logger;
import com.ymt.database.SqlManager;
import com.ymttest.database.model.YmtMessage;
import com.ymttest.database.model.YmtMessageAttach;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/************************************************************************************
 * @File name :DefaultWapper.java
 * @Author : zhouyi
 * @Date : 2015年6月1日
 * @Copyright : 洋码头
 ************************************************************************************/
public class ImDefaultWapper {
	SqlManager manage;


	public ImDefaultWapper() {
		manage = new SqlManager("mysql");
	}
	/**根据msgid查找插入的记录*/
	public List<YmtMessage> selectImInfo(int SellerId,int UserId,int MsgId) throws InterruptedException {
		if(manage==null|| !manage.getEnv().equals("mysql"))
		{
			manage = new SqlManager("mysql");
		}
		int Number=SellerId+UserId;
		Map m = new HashMap();
		m.put("bid", Number%64);
		m.put("msgid", MsgId);
		List<YmtMessage> pia = manage.getSelectResults(
				"com.ymttest.database.mapping.ImDefaultMapper.selectImInfo", m);
		manage.commit();
		return pia;
	}


	/**根据msgid查找插入的记录*/
	public List<YmtMessage> selectImInfoByFromUserIdAndToUserId(int fromUserId,int toUserId) throws InterruptedException {
		if(manage==null|| !manage.getEnv().equals("mysql"))
		{
			manage = new SqlManager("mysql");
		}
		int Number=fromUserId+toUserId;
		Map m = new HashMap();
		m.put("bid", Number%64);
		m.put("fromUserId", fromUserId);
		m.put("toUserId", toUserId);
		List<YmtMessage> pia = manage.getSelectResults(
				"com.ymttest.database.mapping.ImDefaultMapper.selectImInfoByFromUserIdAndToUserId", m);
		manage.commit();
		return pia;
	}

	public List<YmtMessage> selectMessageBySessionNumber(int SellerId,int UserId) throws InterruptedException {
		if(manage==null|| !manage.getEnv().equals("mysql"))
		{
			manage = new SqlManager("mysql");
		}
		int Number=SellerId+UserId;
		Map m = new HashMap();
		m.put("bid", Number%64);
		m.put("sessionkey",SellerId+"_"+UserId);
		List<YmtMessage> pia = manage.getSelectResults(
				"com.ymttest.database.mapping.ImDefaultMapper.selectMessageBySessionNumber", m);
		manage.commit();
		return pia;
	}
	/**根据FromUserId、ToUserId查找记录 ymt_session*/
	public List<Map> selectTotalCount(Integer SellerId,Integer UserId) throws InterruptedException {
		if(manage==null||!manage.getEnv().equals("mysql2"))
		{
			manage = new SqlManager("mysql2");
		}
		Map m = new HashMap();
		m.put("sid", SellerId);
		m.put("uid", UserId);
		m.put("tid", UserId%64);
		//Logger.debug(m);
		@SuppressWarnings({ "rawtypes", "unchecked" })
		List<Map> pia = manage.getSelectResults(
				"com.ymttest.database.mapping.ImDefaultMapper.selectTotalCount", m);
		manage.commit();
		return pia;
	}

	/**根据ObjId来查找记录*/
	public List<Map> selectComment(String ObjId,int ToUserId){
	    manage = new SqlManager("mysql3");
        Map m = new HashMap();
        m.put("ObjId",ObjId);
        m.put("ToUserId",ToUserId%64);
        List<Map> com = manage.getSelectResults(
                "com.ymttest.database.mapping.ImDefaultMapper.selectComment",m);
        manage.commit();
        return com;
    }

	public List<Map> selectCommentByTime(int userId,Long longTimeline,int count,String isNew){
		Date startTime;
		Date endTime;
		int sortType;  //1 asc, -1 desc
		int tableindex;

		if (isNew.equals("No")) {
			startTime = null;
			endTime = longTimeline > 0 ? new Date(longTimeline) : null;
			sortType = -1;
		} else {
			startTime = longTimeline > 0 ? new Date(longTimeline) : null;
			endTime = null;
			sortType = 1;
		}
		tableindex = userId%64;
		manage = new SqlManager("mysql3");
		Map m = new HashMap();

		m.put("tableindex",tableindex);
		m.put("userId",userId);
		m.put("startTime",startTime);
		m.put("endTime",endTime);
		m.put("sortType",sortType);
		m.put("count",count);

		List<Map> com = manage.getSelectResults(
				"com.ymttest.database.mapping.ImDefaultMapper.selectCommentByTime",m);
		manage.commit();
		return com;
	}

	/**根据MessageId来查找*/
	public List<YmtMessageAttach> selectMessageattachByMessageId(String messageId){
		manage = new SqlManager("mysql");
		Map m = new HashMap();
		String[] msgids = messageId.split("_");
		int fromid = Integer.valueOf(msgids[0]);
		int toid = Integer.valueOf(msgids[1]);
		int msgid = Integer.valueOf( msgids[2]);
		String session="";
		if(fromid<toid){
			session=fromid+"_"+toid;
		}else{
			session=toid+"_"+fromid;
		}
		m.put("messageId",msgid);
		m.put("session",session);
		List<YmtMessageAttach> mapList = manage.getSelectResults(
				"com.ymttest.database.mapping.ImDefaultMapper.selectMessageattachByMessageId",m);
		manage.commit();
		return mapList;
	}


	/**根据clientId来查找记录*/
	public List<Map> selectPushBind(String clientId){
		manage = new SqlManager("mysql4");
		Map m = new HashMap();
		m.put("clientId",clientId);
		List<Map> com = manage.getSelectResults(
				"com.ymttest.database.mapping.ImDefaultMapper.selectPushBind",m);
		manage.commit();
		return com;
	}

	public List<Map> selectImNotifyByToUserId(int msgToId){
		manage = new SqlManager("mysql5");
		Map m = new HashMap();
		m.put("tableindex",msgToId%64);
		m.put("MsgToId",msgToId);
		List<Map> com = manage.getSelectResults(
				"com.ymttest.database.mapping.ImDefaultMapper.selectImNotifyByToUserId",m);
		manage.commit();
		return com;
	}

	public List<Map> selectImNotifyByPushTitle(String pushTitle){
		manage = new SqlManager("mysql5");
		Map m = new HashMap();
		m.put("pushTitle",pushTitle);
		List<Map> com = manage.getSelectResults(
				"com.ymttest.database.mapping.ImDefaultMapper.selectImNotifyByPushTitle",m);
		manage.commit();
		return com;
	}

	public List<Map> selectImNotifyByNotifyId(int notifyId){
		manage = new SqlManager("mysql5");
		Map m = new HashMap();
		m.put("notifyId",notifyId);
		List<Map> com = manage.getSelectResults(
				"com.ymttest.database.mapping.ImDefaultMapper.selectImNotifyByNotifyId",m);
		manage.commit();
		return com;
	}

	/**根据deciveId来查找记录*/
	public List<Map> selectPushBindBydeciveId(String deciveId){
		manage = new SqlManager("mysql4");
		Map m = new HashMap();
		m.put("deciveId",deciveId);
		List<Map> com = manage.getSelectResults(
				"com.ymttest.database.mapping.ImDefaultMapper.selectPushBindBydeciveId",m);
		manage.commit();
		return com;
	}

	/**根据deciveId来查找记录*/
	public List<Map> selectPushBindByUserId(int userId){
		manage = new SqlManager("mysql4");
		Map m = new HashMap();
		m.put("userId",userId);
		List<Map> com = manage.getSelectResults(
				"com.ymttest.database.mapping.ImDefaultMapper.selectPushBindByUserId",m);
		return com;
	}


	/**
	 * 通过msgid 查消息
	 *
	 * @param
	 * @return
	 * @throws InterruptedException
	 */
	public List<YmtMessage> selectImInfo(String MessageId) throws InterruptedException {
		if (manage == null || !manage.getEnv().equals("mysql")) {
			manage = new SqlManager("mysql");
		}
		String[] msgids = MessageId.split("_");
		int fromid = Integer.valueOf(msgids[0]);
		int toid = Integer.valueOf(msgids[1]);
		int msgid = Integer.valueOf( msgids[2]);
		int Number = fromid + toid;
		Map m = new HashMap();
		m.put("bid", Number % 64);
		Logger.debug("im tableIndex:"+Number % 64);
		m.put("msgid", msgid);
		List<YmtMessage> pia = manage.getSelectResults("com.ymttest.database.mapping.ImDefaultMapper.selectImInfo", m);
		manage.commit();
		return pia;
	}

	public List<YmtMessage> selectMessageByToUserIdAndFromUserId(int fromUserId,int toUserId) throws InterruptedException {
		if (manage == null || !manage.getEnv().equals("mysql")) {
			manage = new SqlManager("mysql");
		}
		int Number = fromUserId + toUserId;
		Map m = new HashMap();
		m.put("bid", Number % 64);
		m.put("fromUserId", fromUserId);
		m.put("toUserId", toUserId);
		List<YmtMessage> pia = manage.getSelectResults("com.ymttest.database.mapping.ImDefaultMapper.selectMessageByToUserIdAndFromUserId", m);
		manage.commit();
		return pia;
	}

    /**根据userId来查找记录*/
    public List<Map> selectPushConfigByUserId(int userId){
        manage = new SqlManager("mysql4");
        Map m = new HashMap();
        m.put("userId",userId);
        List<Map> com = manage.getSelectResults(
                "com.ymttest.database.mapping.ImDefaultMapper.selectPushConfigByUserId",m);
        manage.commit();
        return com;
    }

	/**根据userId来查找记录*/
	public List<Map> selectSellerPushConfigByUserId(int userId){
		manage = new SqlManager("mysql4");
		Map m = new HashMap();
		m.put("userId",userId);
		List<Map> com = manage.getSelectResults(
				"com.ymttest.database.mapping.ImDefaultMapper.selectSellerPushConfigByUserId",m);
		manage.commit();
		return com;
	}

	// piblic List<Map> select
	/**
	 * 关闭数据库连接
	 */
	public void close() {
		manage.close();
	}

	@Override
	protected void finalize() throws Throwable {
		this.close();
		manage = null;
		super.finalize();
	}

	public static void main(String args[]) throws InterruptedException {
		
	//Logger.debug(new ImDefaultWappe`r().selectImInfo(SellerId, UserId, MsgId, Limit));
	ImDefaultWapper im = new ImDefaultWapper();
	/*Logger.debug(im.selectTotalCount(288484,1113));
	Logger.debug(im.selectTotalCount(1113,288484));*/
//        System.out.println(im.selectCommentByTime(3383, YMTDateUtil.getDate().getTime() - 3600000,5,"Yes"));
//		System.out.println(YMTDateUtil.getDate().getTime());
//		System.out.println(YMTDateUtil.getDate().getTime()-360);
		List<Map> map = im.selectTotalCount(24635724,24838745);
		System.out.println(map);

	}
}
